import Phaser from 'phaser'

/**
 * 启动页面
 * @description
 * 1. 加载预加载页面的资源
 * 2. 设置游戏背景颜色
 * 3. 初始化游戏数据
 */

export default class BootState extends Phaser.State {
  init(...args) {
    console.log('ce---启动Boot页面')
  }

  preload(game) {
    game.stage.backgroundColor = '#a8d4bf'

    game.load.image('bgpreload', 'assets/images/bgpreload.png')
    game.load.image('logo', 'assets/images/logo.png')
    game.load.image('loading', 'assets/images/loading.png')
    game.load.image('progress', 'assets/images/progress.png')
    game.load.image('snail', 'assets/images/snail.png')

    this.errorList = []
    game.load.onFileComplete.add((progress) => {
      if (progress === 100) {
        if (this.errorList.length > 0) {
          console.log(
            'ce---加载资源失败，错误信息：' + JSON.stringify(this.errorList),
          )
        }
      }
    })

    game.load.onFileError.add((key, file) => {
      this.errorList.push({
        key: key,
        errorMessage: file.errorMessage,
      })
    })

    game.load.onLoadComplete.add(() => {})
  }

  create(game) {
    game.state.start('PreloadState')
  }
}
